package com.graduate.examapp.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.graduate.examapp.R
import com.graduate.examapp.entity.ExamTask
import com.graduate.examapp.entity.HistoryData

class HistoryDataAdapter: RecyclerView.Adapter<HistoryDataAdapter.HistoryHolder>() {

    var dataSource: List<HistoryData> = ArrayList()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HistoryHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.historydata_item, parent, false)
        return HistoryHolder(view)
    }

    override fun onBindViewHolder(holder: HistoryHolder, i: Int) {
        val dataText = StringBuilder()
        val historyData = dataSource[i]
        val isForbiddenStr = if(dataSource[i].isForbidden) "是" else "否"
        dataText.append("姓名：")
            .append(historyData.name)
            .append("\n")
            .append("已监考次数：")
            .append(historyData.times)
            .append("\n")
            .append("是否禁用：")
            .append(isForbiddenStr)

        holder.historyDataTextView.text = dataText.toString()
    }

    override fun getItemCount(): Int {
        return dataSource.size
    }

    class HistoryHolder(taskItemView: View) : RecyclerView.ViewHolder(taskItemView) {
        val historyDataTextView = taskItemView.findViewById<TextView>(R.id.history_data_text)
    }

}